package com.ddwanglife.lamda;

import java.util.concurrent.*;
import java.util.stream.IntStream;

public class ParallelDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //测试stream 并行
//        IntStream.range(1,100).parallel().peek(ParallelDemo::debug).count();
        ForkJoinPool fPool = new ForkJoinPool(2);
        ForkJoinTask fR = fPool.submit(()->IntStream.range(1,100).parallel().peek(ParallelDemo::debug).count());

        //如果不执行这个，主线程就结束了，子线程也就结束了
        System.out.println(fR.get());
        fPool.shutdown();
    }
    public static void debug(Integer i){
        //试着打印流是如何定义默认的线程池的
        /**
         * 截取部分打印结果 是由ForkJoinPool实现的，拒绝策略是谁调用反给谁
         * ForkJoinPool.commonPool-worker-2  45
         * main  10
         * ForkJoinPool.commonPool-worker-1  26
         * main  11
         * ForkJoinPool.commonPool-worker-2  46
         * ForkJoinPool.commonPool-worker-3  21
         * ForkJoinPool.commonPool-worker-2  47
         */
        System.out.println(Thread.currentThread().getName()+"  "+i);
    }
}
